package cn.bdqn.brand.controller;

import cn.bdqn.brand.pojo.Brand;
import cn.bdqn.brand.service.BrandService;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "BrandServlet", urlPatterns = "/BrandServlet")
public class BrandServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 编码格式
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        BrandService brandService = new BrandService();

        List<Brand> brands = brandService.showAllBrands();

        // 将list对象转换为json字符串
        String jsonData = JSON.toJSONString(brands);

        response.setContentType("application/json");
        PrintWriter writer = response.getWriter();

        writer.write(jsonData);
    }
}
